package cn.byapps.attendance.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.byapps.attendance.entity.FavJob;
import cn.byapps.attendance.entity.Job;
import cn.byapps.attendance.entity.Student;
import cn.byapps.attendance.entity.XlsStudent;
import cn.byapps.attendance.service.FavJobService;
import cn.byapps.attendance.service.JobService;
import cn.byapps.attendance.service.StudentService;
import cn.byapps.attendance.util.DataTablesResult;
import core.controller.BaseController;
import core.excel.ExportExcel;

@Controller
@RequestMapping("/wechat/favjob")
public class FavJobController extends BaseController {

	@Resource
	private FavJobService favJobService;
	@Resource
	private JobService jobService;
	@Resource
	private StudentService studentService;

	@RequestMapping(value = { "index", "/", "" })
	public String index() {
		return "/wechat/favjob/index";
	}

	@RequestMapping(value = { "list" })
	public String list() {
		return "/wechat/favjob/list";
	}

	@RequestMapping(value = { "stdindex" })
	public String stdlist() {
		return "/person/stdIndex";
	}
	@RequestMapping(value="export",method = RequestMethod.POST)
	public void getXLS(String clazz,String dept,HttpServletResponse response) throws IOException {
		List<XlsStudent> xls = studentService.getXLSData(dept,clazz);
		String fileName= UUID.randomUUID().toString()+".xls";
		new ExportExcel(null, XlsStudent.class, true).setDataList(xls).write(response, fileName).dispose();
	}
	@RequestMapping(value = { "getData" }, method = RequestMethod.POST)
	public void getData(String clazz,String dept,Integer draw, Long start, Long length, HttpServletResponse response) {
		int pos = 0;
		int rows = 10;
		if (start != null) {
			pos = start.intValue();
		}
		if (length != null) {
			rows = length.intValue();
		}
		DataTablesResult<Student> lists = studentService.getData(draw, pos, rows,dept,clazz);
		if (lists == null) {
			lists = new DataTablesResult<Student>();
		}
		try {
			this.writeJSON(response, lists);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = { "getjobs" }, method = RequestMethod.POST)
	public void getJobs(HttpServletResponse response) {
		List<Job> jobs = jobService.listAll();
		if (jobs == null) {
			jobs = new ArrayList<Job>();
		}
		try {
			this.writeJSON(response, jobs);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = { "getecharts" }, method = RequestMethod.POST)
	public void getEcharts(HttpServletRequest request, HttpServletResponse response) {
		Object object = request.getSession().getAttribute("loginUser");
		if (object == null)
			return;
		Student student = (Student) object;

		student = studentService.getById(student.getId());
		Map<String, Object> jobs = favJobService.getEcharts(null, student.getClazz(), 5);
		try {
			this.writeJSON(response, jobs);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "echart", method = RequestMethod.GET)
	public String echart() {
		return "wechat/favjob/echart";
	}
	
	@RequestMapping(value = { "getdataecharts" }, method = RequestMethod.POST)
	public void getDataEcharts(String clazz,String dept,HttpServletRequest request, HttpServletResponse response) {

		Map<String, Object> jobs = favJobService.getEcharts(dept, clazz, 15);
		try {
			this.writeJSON(response, jobs);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "wechat/favjob/add";
	}

	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(String id, Model model) {
		FavJob ad = favJobService.get(id);
		model.addAttribute("item", ad);
		return "wechat/favjob/update";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(String id, Integer first, Integer second, Integer third, String cellphone) {
		FavJob fav = favJobService.get(id);
		if (first != null) {
			fav.setFirstJob(jobService.get(first));
		} else {
			fav.setFirstJob(null);
		}
		if (second != null) {
			fav.setSecondJob(jobService.get(second));
		} else {
			fav.setSecondJob(null);
		}
		if (third != null) {
			fav.setThirdJob(jobService.get(third));
		} else {
			fav.setThirdJob(null);
		}
		if (cellphone != null && !cellphone.equals("")) {
			fav.setCellphone(cellphone);
		}
		favJobService.update(fav);
		return "redirect:stdindex";
	}

	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(String id) {
		FavJob fav = new FavJob();
		fav.setId(id);
		favJobService.delete(fav);
		return "redirect:list";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Integer first, Integer second, Integer third, String cellphone, HttpServletRequest request)
			throws IOException {

		Object object = request.getSession().getAttribute("loginUser");
		if (object == null)
			return "redirect:stdindex";
		Student student = (Student) object;

		student = studentService.getById(student.getId());
		if (student == null)
			return "redirect:stdindex";

		FavJob fav = student.getFavJob();
		if (fav == null) {
			fav = new FavJob();
			if (first != null) {
				fav.setFirstJob(jobService.get(first));
			}
			if (second != null) {
				fav.setSecondJob(jobService.get(second));
			}
			if (third != null) {
				fav.setThirdJob(jobService.get(third));
			}
			if (cellphone != null && !cellphone.equals("")) {
				fav.setCellphone(cellphone);
			}
			fav.setStudent(student);

			favJobService.save(fav);
		} else {
			if (first != null) {
				fav.setFirstJob(jobService.get(first));
			}
			if (second != null) {
				fav.setSecondJob(jobService.get(second));
			}
			if (third != null) {
				fav.setThirdJob(jobService.get(third));
			}
			if (cellphone != null && !cellphone.equals("")) {
				fav.setCellphone(cellphone);
			}
			favJobService.update(fav);
		}

		return "redirect:stdindex";
	}
}
